*Emergency Department Cell Data
use "`base'\Data Files and Code That Produced Them\P03 ED Analysis File", replace

*Population underlying each cell varies slightly across observations
gen pop_all_np =  1 + 0.00104*(months_21/12)
gen pop_m      =  1 + 0.00016718*(months_21/12)
gen pop_f_np   =  1 + 0.00196*(months_21/12)
 
local sufs = "all all_np f f_np m"  
local vars  = "visit  illness injury_or_alc  alcohol_any inj_by_self inj_by_oth inj_accident   "

*Create all fitted lines on either side of RD
foreach suf of local sufs {
   foreach var of local vars {  
     *Fit the left side polynomial;
      reg `var'_`suf'_r age_c age_c_sq [aweight=pop_`suf'] if age_c >= -2 & age_c < 0, robust
      predict `var'_`suf'_left        if age_c >= -2 & age_c <= 0
     *Fit the right side polynomial;
      reg `var'_`suf'_r age_c age_c_sq [aweight=pop_`suf'] if age_c > 0 & age_c < 2, robust
      predict `var'_`suf'_right       if age_c >= 0 & age_c < 2
   }
}
  
*Restrict the sample
keep if age_months >= 21 - 2  & age_months < 21 + 2 


#delimit ;
graph twoway  
 	        (scatter injury_or_alc_all_np_r age_months, mcolor(blue)  msymbol(O)  msize(.6) yaxis(1) 
			   yscale(range(1000,2000) axis(1)) 
			   ylabel(#3, axis(1) nogrid)) 
			(line injury_or_alc_all_np_left age_months, lwidth(thin) lcolor(blue) yaxis(1)) 
            (line injury_or_alc_all_np_right age_months, lwidth(thin) lcolor(blue) yaxis(1)) 
			  
	        (scatter illness_all_np_r age_months, mcolor(black)  msymbol(Sh)  msize(.8) yaxis(2)  
			   yscale(range(2000,3000) axis(2)) 
			   ylabel(#3, axis(2))) 
            (line illness_all_np_left age_months, lwidth(thin) lcolor(black) yaxis(2)) 
            (line illness_all_np_right age_months, lwidth(thin) lcolor(black) yaxis(2)) 
             , 
			 title("Figure 1: Emergency Department Visit Rates",size(medlarge)) 
			 xtitle("Age at Time of ED Visit",size(medlarge))  
			 ytitle("Not Injury or Intoxication",axis(2) size(medlarge)) 
			 ytitle("Injury or Intoxication", axis(1) size(medlarge)) 
			 legend(off) 
			 text(1900 20 "Not Injury or Intoxication", color(black) size(medlarge)) 
			 text(1350 20 "Injury or Alcohol Intoxication",color(blue) size(medlarge)) 
			 graphregion(style(none) color(gs16))  
 
      ;
#delimit cr
grexportpdf "`base'\Code for Figures and Tables in Paper\Figure 1.pdf"

